﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;

namespace HttpRequestUpdater
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] arrayUrl = {
            "http://www.cqpds.com:9016/version/AutoUpdater.dll",
            "http://www.cqpds.com:9016/version/E3Setup.exe",
            "http://www.cqpds.com:9016/version/AutoUpdater.config"
        };
            Thread.Sleep(3000);
            foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
            {
                if (p.ProcessName == "E3Setup")
                {
                    p.Kill();
                }
            }


            for (int i = 0; i < arrayUrl.Length; i++)
            {
                Uri downUri = new Uri(arrayUrl[i]);

                int ix = arrayUrl[i].LastIndexOf("/");

                //建立一个ＷＥＢ请求，返回HttpWebRequest对象           
                HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(downUri);
                //设置接收对象的范围为0-10000000字节。

                hwr.AddRange(0, 10000000);

                //流对象使用完后自动关闭
                using (Stream stream = hwr.GetResponse().GetResponseStream())
                {
                    //文件流，流信息读到文件流中，读完关闭@"D:\E3Setup"
                    using (FileStream fs = File.Create(Environment.CurrentDirectory + "\\"+arrayUrl[i].Substring(ix+1, arrayUrl[i].Length-ix-1)))
                    {
                        //建立字节组，并设置它的大小是多少字节
                        byte[] bytes = new byte[102400];
                        int n = 1;
                        while (n > 0)
                        {
                            //一次从流中读多少字节，并把值赋给Ｎ，当读完后，Ｎ为０,并退出循环
                            n = stream.Read(bytes, 0, 10240);
                            fs.Write(bytes, 0, n); //将指定字节的流信息写入文件流中
                        }
                    }
                }
            }
        }
    }
}
